package com.wyfx.blockchain.utils;

import com.wyfx.blockchain.common.exception.BaseException;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

/**
 * @author johnson liu
 * @description 断言工具类
 * @date 2020/8/4 10:03
 */
public abstract class AbstractAssertUtil {

    /**
     * 如果入参collection集合为空，则抛出异常
     * @param collection
     * @
     */
    public static void notEmpty(@Nullable Collection collection, String message){
        if (ObjectUtils.isEmpty(collection)) {
            throw new BaseException(message);
        }
    }

    /**
     * 如果入参对象为null，抛出异常
     * @param object
     * @
     */
    public static <T> T notNull(T object,String message){
        if (object == null) {
            throw new BaseException(message);
        }
        return object;
    }

    /**
     * 如果入参text为空，则抛出异常
     * @param text
     * @
     */
    public static void hasText(@Nullable String text,String message){
        if (!StringUtils.hasText(text)) {
            throw new BaseException(message);
        }
    }

    /**
     * 断言是否为真，如果为 {@code false} 抛出 {@code BusinessException} 异常<br>
     * @param expression 布尔值
     * @param message 错误码
     * @ if expression is {@code false}
     */
    public static void isTrue(boolean expression, String message){
        if (false == expression) {
            throw new BaseException(message);
        }
    }

    /**
     * 断言是否为假，如果为 {@code true} 抛出 {@code BusinessException} 异常<br>
     * @param expression 布尔值
     * @param message 错误码
     * @ if expression is {@code false}
     */
    public static void isFalse(boolean expression, String message){
        if (expression) {
            throw new BaseException(message);
        }
    }

    public static void getForObject(RestTemplate restTemplate,String url,String message,Object obj){

    }

}
